#ifndef EVENT_LOOP_H
#define EVENT_LOOP_H

#include <pthread.h>
#include "channel.h"
#include "common.h"
#include "event_dispatcher.h"

extern const struct event_dispatcher epoll_dispatcher; // epoll 派发器

struct channel_element
{
    int type; // 1 add; 2 delete
    struct channel *chan;
    struct channel_element *next;
};

struct event_loop
{
    int quit;
    const struct event_dispatcher *dispatcher;

    void *event_dispatcher_data;
    struct channel_map *chan_map;

    int is_handle_pending;
    struct channel_element *pending_head;
    struct channel_element *pending_tail;

    pthread_t owner_thread_id;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int socketPair[2]; // 用于 acceptor 与 sub-reactor 通信
    char *thread_name;
};

struct event_loop *event_loop_init();

struct event_loop *event_loop_init_with_name(char *threadname);

int event_loop_run(struct event_loop *eloop);

void event_loop_wakeup(struct event_loop *eloop);

int event_loop_add_channel_event(struct event_loop *eloop, int fd, struct channel *chan);
int event_loop_remove_channel_event(struct event_loop *eloop, int fd, struct channel *chan);
int event_loop_update_channel_event(struct event_loop *eloop, int fd, struct channel *chan);

int event_loop_handle_pending_add(struct event_loop *eloop, int fd, struct channel *chan);
int event_loop_handle_pending_remove(struct event_loop *eloop, int fd, struct channel *chan);
int event_loop_handle_pending_update(struct event_loop *eloop, int fd, struct channel *chan);

int channel_event_activate(struct event_loop *eloop, int fd, int res);

#endif